//创建抽象类Employee
abstract class Employee {
    String name;
    double salary;
    //构造函数
    public Employee(String n, double s) {
        this.name = n;
        this.salary = s;
    }
    //纯虚方法，计算工资
    public abstract double calculateSalary();
}
//月薪员工类SalariedEmployee
class SalariedEmployee extends Employee {
    public SalariedEmployee(String n, double s) {
        super(n, s);
    }
    //直接返回月薪
    @Override
    public double calculateSalary() {
        return salary;
    }
}
//时薪员工类HourlyEmployee
class HourlyEmployee extends Employee {
    private double hoursWorked;

    public HourlyEmployee(String n, double s, double hours) {
        super(n, s);
        this.hoursWorked = hours;
    }
    //根据工作小时数计算工资
    @Override
    public double calculateSalary() {
        return salary * hoursWorked;
    }
    //获取工作小时数
    public double getHoursWorked() {
        return hoursWorked;
    }
}
//主函数
class Main {
    public static void main(String[] args) {
        //创建员工数组，存储指向 Employee 的指针
        Employee[] employees = new Employee[5];
        //创建几个员工对象
        employees[0] = new SalariedEmployee("王林", 5000);//月薪员工
        employees[1] = new HourlyEmployee("罗峰", 20, 160);//时薪员工，工作160小时
        employees[2] = new SalariedEmployee("叶凡", 4500);//月薪员工
        employees[3] = new HourlyEmployee("陈平安", 15, 120);//时薪员工，工作120小时
        employees[4] = new SalariedEmployee("石昊", 6000);//月薪员工
        //输出每个员工的姓名、工资和工作时长
        for (Employee e : employees) {
            //输出员工姓名
            System.out.print("员工: " + e.name + " ");
            //输出工资
            System.out.print("工资: " + e.calculateSalary());
            //如果是时薪员工，输出工作时长
            if (e instanceof HourlyEmployee) {
                HourlyEmployee h = (HourlyEmployee) e;
                System.out.print("，工作时长: " + h.getHoursWorked() + " 小时");}
            System.out.println();
        }
    }
}
